package com.atguigu.gmall.cart.service;

import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.model.cart.CartInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class CartInfoSyncServiceImpl implements CartInfoSyncService {
    @Autowired
    private CartInfoMapper cartInfoMapper;

    //异步保存购物车数据到DB
    @Override
    @Async
    public void insertCartInfo(CartInfo cartInfo) {
        System.out.println("异步保存线程名" + Thread.currentThread().getName());
        cartInfoMapper.insert(cartInfo);
    }

    @Override
    @Async
    public void updateCartInfo(CartInfo cartInfo) {
        System.out.println("异步修改线程名" + Thread.currentThread().getName());
        cartInfoMapper.update(cartInfo, new QueryWrapper<CartInfo>().
                eq("user_id", cartInfo.getUserId()).
                eq("sku_id", cartInfo.getSkuId()));
    }

    @Override
    @Async
    public void deleteCartInfo(String userId, Long skuId) {
        cartInfoMapper.delete(new QueryWrapper<CartInfo>().
                eq("user_id", userId).
                eq("sku_id", skuId));
    }
}
